package problems;


public class Euler012Smarter extends AbstractEuler {

	private static final int UPPER_BOUND = 100000; 
	
	@Override
	public Number calculate() {
		for(int i = 1, iThTriangularNumber = 0; i <= UPPER_BOUND; i++) {
			iThTriangularNumber = iThTriangularNumber + i;
			
			int factors = 0;

			//you need only check for factors up to the square root of the triangle number - each number above has a mirror factor below.
			for (int n = 1; n <= Math.sqrt(iThTriangularNumber); n++) {
				if (iThTriangularNumber % n == 0) factors = factors + 2;
			}
			
			if (factors >= 500) return iThTriangularNumber;
			
		}

		fail("answer not found within the the first " + UPPER_BOUND + " triangular numbers"); return null;
	}

	@Override
	protected Number getCorrectAnswer() {
		return 76576500;
	}

}
